package com.simple.component;

import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.CCUtil;
import com.simple.R;
import com.toast.ToastUtils;

/**
 * Copyright (C), 2016-2020,呼我出行网络科技有限公司
 * FileName: LoginActivity
 * Author: Administrator
 * Date: 2020/11/3 17:28
 * Description:
 * History:
 */
public class LoginActivity extends AppCompatActivity {
    EditText editText;
    Button login;
    private String callId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        callId = getIntent().getStringExtra(CCUtil.EXTRA_KEY_CALL_ID);
        if (TextUtils.isEmpty(callId)) {
            callId = CCUtil.getNavigateCallId(this);
        }
        editText = findViewById(R.id.account_et);
        login = findViewById(R.id.login_btn);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editText.getText().toString().length() < 6) {
                    ToastUtils.showToast(LoginActivity.this, "请正确输入内容");
                    return;
                }
                Global.loginUserName = editText.getText().toString();
                CC.sendCCResult(callId, CCResult.success(Global.KEY_USERNAME, Global.loginUserName));
                callId = null;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 200);
            }
        });
    }

    @Override
    protected void onDestroy() {
        //判断是否为CC调用打开本页面
        if (callId != null) {
            //为确保不管登录成功与否都会调用CC.sendCCResult，在onDestroy方法中调用
            CC.sendCCResult(callId, CCResult.error("login canceled"));
        }
        super.onDestroy();
    }
}
